function formatDate(metricTime) {
    var year = metricTime.getFullYear().toString();
    var month = (metricTime.getMonth()+1).toString();
    if (month.length==1) {
        month = "0" + month;
    }
    var date = metricTime.getDate().toString();
    if (date.length==1) {
         date = "0" + date;
    }
    var hour = metricTime.getHours().toString();
    if (hour.length==1) {
        hour = "0" + hour;
    }
    var minute = metricTime.getMinutes();
    minute = (15*Math.floor(minute/15)).toString();
    if (minute.length==1) {
        minute = "0" + minute;
    }
    var queryTime = year+month+date+hour+minute;
    return queryTime;
}
var staticContinuePeriod = parseInt(execution.getVariable("staticContinuePeriod"));
var staticDiscontinuePeriod = parseInt(execution.getVariable("staticDiscontinuePeriod"));
var relativeContinuePeriod = parseInt(execution.getVariable("relativeContinuePeriod"));
var relativeDiscontinuePeriod = parseInt(execution.getVariable("relativeDiscontinuePeriod"));
var staticAnomalyTimes = parseInt(execution.getVariable("staticAnomalyTimes"));
var staticThreshold = parseFloat(execution.getVariable("staticThreshold"));
var relativeThreshold = parseFloat(execution.getVariable("relativeThreshold"));
var relativeDiscontinueAnomalyTimes = parseInt(execution.getVariable("relativeDiscontinueAnomalyTimes"));
var dodThreshold = parseFloat(execution.getVariable("dodThreshold"));
var configPeriods = [staticContinuePeriod, staticDiscontinuePeriod, relativeContinuePeriod, relativeDiscontinuePeriod];
var maxPeriod = 0;
for (var i=0;i<configPeriods.length;i++) {
    if (maxPeriod<configPeriods[i]) {
        maxPeriod = configPeriods[i];
    }
}
var endTime = execution.getVariable("endTime");
var periods = [];
var endDateTime = new Date(endTime.substring(0,4)+"-"+endTime.substring(4,6)+"-"+endTime.substring(6,8)+" "+endTime.substring(8,10)+":"+endTime.substring(10,12)+":00");
for (var i=1;i<(maxPeriod+1);i++) {
    periods.push(formatDate(new Date(endDateTime-15*i*60*1000)));
}
var neNameList = execution.getVariable("neNameList");
var neNames = neNameList.split(",");
var metrics = JSON.parse(execution.getVariable("metrics"));
var metricsHistory = JSON.parse(execution.getVariable("metricsHistory"));
var historyStartTime = execution.getVariable("historyStartTime");
var alarmNE = [];
for (var i=0;i<neNames.length;i++) {
    var neMetric = metrics[neNames[i]];
    if (neMetric) {
        //判断是否在静态周期内连续超出静态门限
        var count = 0;
        for (var j=0;j<staticContinuePeriod;j++) {
            if (neMetric[periods[j]]) {
                if (parseFloat(neMetric[periods[j]])<staticThreshold) {
                    count++;
                }
            }
        }
        if (count==staticContinuePeriod) {
            alarmNE.push(neNames[i]);
            continue;
        }
        //判断是否在静态周期内非连续超出静态门限
        var count = 0;
        for (var j=0;j<staticDiscontinuePeriod;j++) {
            if (neMetric[periods[j]]) {
                if (parseFloat(neMetric[periods[j]])<staticThreshold) {
                    count++;
                }
            }
        }
        if (count>=staticAnomalyTimes) {
            alarmNE.push(neNames[i]);
            continue;
        }
        //判断是否在环比周期内连续超出门限
        var count = 0;
        for (var j=0;j<relativeContinuePeriod;j++) {
            if (neMetric[periods[j]] && neMetric[periods[j+1]]) {
                if ((parseFloat(neMetric[periods[j+1]])*1000 - parseFloat(neMetric[periods[j]])*1000)>=relativeThreshold*1000) {
                    count++;
                }
            }
        }
        if (count==relativeContinuePeriod) {
            alarmNE.push(neNames[i]);
            continue;
        }
        //判断是否在环比周期内非连续超出门限
        var count = 0;
        for (var j=0;j<(relativeDiscontinuePeriod);j++) {
            if (neMetric[periods[j]] && neMetric[periods[j+1]]) {
                if ((parseFloat(neMetric[periods[j+1]])*1000 - parseFloat(neMetric[periods[j]])*1000)>=relativeThreshold*1000) {
                    count++;
                }
            }
        }    
        if (count>=relativeDiscontinueAnomalyTimes) {
            alarmNE.push(neNames[i]);
            continue;
        }
        //判断是否同比超出下降门限
        if (neMetric[periods[0]] && metricsHistory[neNames[i]][historyStartTime]) {
            if ((parseFloat(metricsHistory[neNames[i]][historyStartTime])*1000 - parseFloat(neMetric[periods[0]])*1000)>dodThreshold*1000) {
                alarmNE.push(neNames[i]);
                continue;
            }
        }    
    }
}
var result = [];
var d = new Date();
var alarmTime = d.getFullYear().toString()+"-"+(d.getMonth()+1).toString()+"-"+d.getDate().toString()+" "+d.getHours().toString()+":"+d.getMinutes().toString()+":00";
var ruleName = execution.getVariable("ruleName");
for (var i=0;i<alarmNE.length;i++) {
    result.push({"alarmTime": alarmTime, "alarmTitle": ruleName, "neName": alarmNE[i]});
}
execution.setVariable("alarmNE", JSON.stringify(result));
result = [];
var noAlarmNE = [];
if (alarmNE.length>0) {
    for (var i=0;i<neNames.length;i++) {
        if (alarmNE.indexOf(neNames[i])<0) {
            noAlarmNE.push(neNames[i]);
        }
    }
} else {
    noAlarmNE = neNames;
}
for (var i=0;i<noAlarmNE.length;i++) {
    result.push({"alarmTitle": ruleName, "neName": noAlarmNE[i]});
}
execution.setVariable("noAlarmNE", JSON.stringify(result));